package com.cnyz.controller;

import com.cnyz.base.PageResponse;
import com.cnyz.base.ResponseResult;
import com.cnyz.dto.CourseDto;
import com.cnyz.service.CourseService;
import com.cnyz.service.CustomerService;
import com.cnyz.vo.CourseVo;
import com.cnyz.vo.UserVo;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
@ApiModel(description = "课程接口")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CustomerService customerService;

    @GetMapping("/teacherid")
    public List<CourseVo> getCourseByTeacherId(@RequestParam("teacherID") Long teacherID) {
        return courseService.getCourseByTeacherID(teacherID);
    }

    @GetMapping("/studentid")
    public List<CourseVo> getCourseByStudentId(@RequestParam("studentID") Long studentID) {
        return courseService.getCourseByStudentID(studentID);
    }

    @PostMapping("/insert")
    public ResponseResult insertCourse(@RequestBody CourseDto courseDto) {
        Long id = courseService.insertCourse(courseDto);
        return ResponseResult.success("新增课程成功！",id);
    }

    @PostMapping("/update")
    public ResponseResult updateCourse(@RequestBody CourseDto courseDto) {
        courseService.updateCourse(courseDto);
        return ResponseResult.success("更新课程信息成功！");
    }

    @DeleteMapping("/delete")
    public ResponseResult deleteCourse(@RequestParam Long courseID) {
        courseService.deleteCourse(courseID);
        return ResponseResult.success("删除课程成功！");
    }

    @PostMapping("/add-student")
    public ResponseResult addStudent(@RequestParam Long courseID, @RequestParam Long studentID) {
        UserVo stu =  customerService.getUserById(studentID);
        if(stu == null || stu.getRole() != 0) {
            return ResponseResult.error("无效的学生ID！");
        }
        if(courseService.isStudentInCourse(courseID, studentID)) {
            return ResponseResult.error("学生已经加入课程！");
        }
        courseService.addStudentToCourse(courseID, studentID);
        return ResponseResult.success("学生"+stu.getUsername()+"加入课程！");
    }

    @GetMapping("/members")
    public ResponseResult<PageResponse<UserVo>> getMembers(
            @RequestParam(value = "page", defaultValue = "1") int pageNum,
            @RequestParam(value = "size", defaultValue = "10") int pageSize,
            @RequestParam Long courseID
    ){
        return ResponseResult.success("查询学生成功",courseService.getStudentByCourseID(pageNum, pageSize, courseID));
    }

    @GetMapping("/members/id")
    public ResponseResult<PageResponse<UserVo>> getMembersById(@RequestParam Long courseID, @RequestParam Long studentID) {
        UserVo stu =  customerService.getUserById(studentID);
        if(stu == null || stu.getRole() != 0) {
            return ResponseResult.error("无效的学生ID！");
        }
        if(!courseService.isStudentInCourse(courseID, studentID)) {
            return ResponseResult.error("学生不在课程中！");
        }
        return ResponseResult.success(stu);
    }
    @GetMapping("/members/name")
    public ResponseResult<PageResponse<UserVo>> getMembersByName(
            @RequestParam(value = "page", defaultValue = "1") int pageNum,
            @RequestParam(value = "size", defaultValue = "10") int pageSize,
            @RequestParam Long courseID,
            @RequestParam String username
    ) {
        return ResponseResult.success(courseService.getMembersByName(pageNum, pageSize, courseID, username));
    }

    @DeleteMapping("/remove-student")
    public ResponseResult removeStudent(@RequestParam Long courseID, @RequestParam Long studentID) {
        UserVo stu =  customerService.getUserById(studentID);
        if(stu == null || stu.getRole() != 0) {
            return ResponseResult.error("无效的学生ID！");
        }
        if(!courseService.isStudentInCourse(courseID, studentID)) {
            return ResponseResult.error("学生不在课程中！");
        }
        courseService.removeStudentFromCourse(courseID, studentID);
        return ResponseResult.success("已移除学生"+stu.getUsername()+"！");
    }


}
